home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
ee1091.zip
/
EE.C
next >
Wrap
Text File
|
1991-10-11
|
22KB
|
1,077 lines
/* ee.c, Yijun Ding, Copyright 1991
Compile with TC: tcc ee.c
Compile in unix: gcc ee.c -lcursesX
Prototype are listed in the order defined. They are essentially grouped
as cursor move, screen, search, files, misc, mainloop, main.
*/
#include <stdio.h>
#include <string.h>
#define EOL '\0' /* end of line marker */
#define BLK ' ' /* blank */
#define LF '\n' /* new line */
#define NLEN 80 /* buffer line for file name */
#define LMAX 10000 /* max line length */
#define XINC 20 /* increament of x */
#define HLP 28
#define CHG 0
#define FIL 1 /* fill */
#define OVR 2 /* insert */
#define CAS 3 /* case sensative */
#define TAB 4 /* tab */
#define POS 5 /* show pos */
#define ALT 6 /* meta_key */
#define SHW 7 /* screen */
#define NEW 8 /* new file */
#define EDT 9 /* quit edit */
#define WIN 10 /* window */
#define NTS 11 /* note posted */
#define ALL 12 /* last flag, quit */
char flag[ALL+1];
char fsym[]="|foctp~";
typedef struct {
char name[NLEN];
int jump;
} MWIN; /* my window structure */
/* order of \n\r is important for curses */
#define HELP_STR "\n\
Usage: ee [-+line(def=1) -ttab(def=2) file(def=Notes)]\n\r\
\n\
A set mark, append file; B format, right margin;\n\r\
C shell, execute line; D cursor right, line end;\n\r\
E cursor up, file top; F forward search, backward search;\n\r\
G goto line, goto column; H delete before, delete under;\n\r\
I tab, tab insert; J play macro, record;\n\r\
K block copy, block cut; L refresh screen, status;\n\r\
M return; N toggle insert mode;\n\r\
O toggle mode; P command prefix;\n\r\
Q command prefix; R replace, replace all;\n\r\
S cursor left, line begin; T delete word, del eol;\n\r\
U block paste, block write; V page down, page up;\n\r\
W window, window cancel; X cursor down, file bottom;\n\r\
Y block delete line; Z exit, edit other file;\n\r\
\n\
Each letter represent a control key. Each key corresponds one or two\n\r\
editing commands (seperated by comma). Press the control key for the\n\r\
first editing function, or control-P and the control key for the second\n\r\
editing function.\n\n\r\
Press any key to continue ..."
#ifdef __TURBOC__
#define AMAX 0xD000 /* main buffer size */
#define BMAX 0x400 /* block size */
#define YTOP 1
#define ttopen() sww=76;swhfull=23
/* #define highvideo() textattr(0x30)
#define lowvideo() textattr(0x1F)
#define ttclose() textattr(0x07) */
#define ttclose() normvideo()
/* honest get a key */
int get_key()
{
static char k1[]="GHIKMOPQRS";
static char k2[]="sEvSDdXVNh";
int key;
char *s;
if((key=getch()) == 0) {
if((s=strchr(k1, getch())) == 0)
return HLP;
if((key = k2[s-k1]) > 'a')
flag[ALT]++;
return key&0x1F;
}
return key;
}
#else
#include "eeibm.c"
#endif
char bbuf[10]; /* backup file name */
char sbuf[NLEN], rbuf[NLEN]; /* search buffer, replace buffer */
char *ae, aa[AMAX]; /* main buffer */
char bb[BMAX], *mk; /* block buffer, mark */
unsigned blen;
char *dp, *ewb; /* current pos, line */
int xtru, ytru; /* file position */
int ytot; /* 0 <= ytru <= ytot */
int swhfull; /* screen physical height */
int x, sww; /* screen position 1 <= x <= sww */
int y, swh; /* screen size 0 <= y <= swh */
int y1, y2; /* 1st, 2nd line of window */
int tabsize=8; /* tab size */
FILE *fi, *fo;
MWIN win, winnext, wincopy; /* current, next, other windows */
void cursor_up(), cursor_down(), cursor_left(), cursor_right();
void show_rest(int len, char *s);
void show_scr(int fr, int to);
void show_sup(int line), show_sdn(int line);
void show_flag(int x, int g);
void show_note(char *prp);
int show_gets(char *prp, char *buf);
void show_top(), show_help(), show_mode(), show_status();
void file_read();
char *file_ltab(char *s);
int file_write(FILE *fp, char *s, char *e);
int file_fout();
void file_save(int f_all, int f_win);
void file_more();
void file_rs(char *s, char *d);
void goto_x(int xx), goto_y(int yy);
void goto_ptr(char *s);
void goto_row(), goto_col();
int str_cmp(char *s);
char *goto_find(char *s, int back);
void goto_search(int back);
void goto_replace(int whole);
void window_other(), window_cancel(), window_size();
void block_put(), block_get(), block_mark();
void block_copy(int delete);
void block_paste(), block_write(), block_line();
int block_fill();
void block_format();
void key_return(), key_deleol(char *s), key_delete();
void key_backspace(), key_delword(int eol);
void key_tab(int tabi);
void key_normal(int key);
void key_shell(int line);
void key_macros(int record);
void main_meta(int key), main_exec(int key);
void main_loop(), main(int argc, char **argv);
/* cursor movement ----------------------------------------- */
void cursor_up()
{
if(ytru == 0) return;
ytru--;
while(*--ewb != EOL) ;
y--;
}
void cursor_down()
{
if(ytru == ytot) return;
ytru++;
while(*++ewb != EOL) ;
y++;
}
/* cursor left & right: x, xtru */
void cursor_left()
{
if(xtru == 1) return;
xtru--;
if(--x < 1) {
x += XINC;
flag[SHW]++;
}
}
void cursor_right()
{
if(xtru == LMAX) return;
xtru++;
if(++x > sww) {
x -= XINC;
flag[SHW]++;
}
}
#define cursor_pageup() {int i; for(i=1; i<swh; ++i) cursor_up();}
#define cursor_pagedown(){int i; for(i=1; i<swh; ++i) cursor_down();}
/* dispaly --------------------------------------------------------*/
/* assuming cursor in correct position: show_rest(sww-x,ewb+xtru) */
void show_rest(len, s)
int len;
char *s;
{
char save;
save = s[len];
s[len] = 0;
cputs(s);
s[len] = save;
clreol();
}
/* ewb and y correct */
void show_scr(fr,to)
int fr, to;
{
char *s=ewb;
int len=sww-1, i=fr;
unsigned xl=xtru-x;
/* calculate s */
for(; i<y; i++) while(*--s != EOL) ;
for(; i>y; i--) while(*++s != EOL) ;
/* first line */
s++;
do {
gotoxy(1, fr+y2);
if(s<ae && strlen(s) > xl) show_rest(len, s+xl);
else clreol();
while(*s++) ;
} while(++fr <= to);
}
void show_sup(line)
int line;
{
gotoxy(1, y2+line);
delline();
show_scr(swh, swh);
}
void show_sdn(line)
int line;
{
gotoxy(1, y2+line);
insline();
show_scr(line, line);
}
void show_flag(x, g)
int x, g;
{
gotoxy(14+x, y1);
putch(g? fsym[x]: '.');
flag[x] = g;
}
void show_note(prp)
char *prp;
{
gotoxy(17+ALT, y1);
cputs(prp);
clreol();
flag[NTS]++;
}
int show_gets(prp, buf)
char *prp, *buf;
{
int key;
int col=(-1);
show_note(prp);
cputs(": ");
cputs(buf);
for(;;) {
key = get_key();
if(key >= BLK) {
if(col < 0) {
col++;
show_note(prp);
cputs(": ");
}
buf[col++] = key;
}
else if(key == 8) {
if(col < 0) col = strlen(buf);
if(col == 0) continue;
col--;
}
else break;
putch(key);
}
flag[ALT] = 0;
if(col > 0) buf[col] = 0;
return (key == 27 || *buf == 0);
}
void show_top()
{
int i;
gotoxy(1, y1);
highvideo();
cputs(win.name);
lowvideo();
clreol();
for(i=0; i<=ALT; i++)
show_flag(i, flag[i]);
flag[NTS]++;
}
void show_help()
{
clrscr();
cputs(HELP_STR);
get_key();
show_top();
flag[SHW]++;
}
void show_mode()
{
char *d;
int k;
show_note(fsym);
putch(BLK);
k = get_key()|0x60;
if((d=strchr(fsym, k)) != 0) {
k = d-fsym;
show_flag(k, !flag[k]);
}
}
void show_status()
{
char tbuf[80];
sprintf(tbuf, "line %d/%d, col %d, char %u/%u/%u+%u",
ytru+1, ytot, xtru, dp-aa, ae-aa, BMAX, AMAX);
show_note(tbuf);
sleep(1);
}
/* file operation ---*/
void file_read()
{
int c;
char *col;
ewb = aa;
ae = mk = col = aa+1;
xtru = x = 1;
ytru = y